import os
import time
from tools.lock.file_lock import FileLock
import shutil
from tools.log import logger

def work(gloabal_conf,self_conf):
    cnt=1
    lock_file = gloabal_conf["environment"]+gloabal_conf["share"]["recycle"]["lock"]
    recycle_regedit = gloabal_conf["environment"]+gloabal_conf["share"]["recycle"]["file"]
    lock = FileLock(lockfile=lock_file)
    sleep_ttk = self_conf["recycle"]["ring_time"]
    while True:
        with lock:
            recycle_list=None
            with open(recycle_regedit,"r",encoding='utf-8') as fd:
                recycle_list = fd.readlines()
            for item in recycle_list:
                item = item.strip("\n")
                if os.path.isfile(item):
                    os.remove(item)  # 删除文件
                elif os.path.isdir(item):
                    shutil.rmtree(item)  # 删除目录及其内容
                else:
                    logger.warning(f"'{item}' 不存在或无法识别")           
            with open(recycle_regedit,"w",encoding='utf-8') as wd:
                wd.write("")
        logger.info(f"第{cnt}次文件清除完成")
        cnt+=1
        time.sleep(sleep_ttk) # 定时执行
    